/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufg.inf.pos.bd;

import br.ufg.inf.pos.br.utils.ConnectionFactory;
import br.ufg.inf.pos.br.utils.RegraDaoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 *
 * @author luan
 */
public class FuncionarioDaoImpl implements FuncionarioDao{
    
    private Connection connection = null;
    private Statement statement = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;
    
    private final String COLUMN_IDACESSO = "IDUSUARIO";
    private final String COLUMN_LOGIN = "LOGINUSUARIO";
      private final String COLUMN_SENHA = "SENHAUSUARIO";
    private final String COLUMN_NOME = "NOMEUSUARIO";
      private final String COLUMN_AUTORIZACAO = "AUTORIZACAOUSUARIO";
    

    @Override
    public void salvar(Funcionario funcionario) {
       
            
    }

    @Override
    public Funcionario getByName(String usuario) {
           throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
 
    }

    @Override
    public List<Funcionario> getAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void atualizar(String id, Funcionario funcionario) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void remover(Funcionario funcionario) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public Funcionario validarAcesso(Funcionario funcionario) throws RegraDaoException{
             Funcionario funcionarioLido = null;
        try {
        connection = ConnectionFactory.getConnection();
      
        
            String sql = "select * from funcionario where "+COLUMN_LOGIN+" = ? and "+COLUMN_SENHA+" = ?";
         
        preparedStatement = connection.prepareStatement(sql);
             
           preparedStatement.setString(1, funcionario.getLogin());
            preparedStatement.setString(2, funcionario.getSenha());
            preparedStatement.executeUpdate();
            resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
                funcionarioLido = new Funcionario();
                funcionarioLido.setLogin(resultSet.getString(COLUMN_LOGIN));
                funcionarioLido.setSenha(resultSet.getString(COLUMN_SENHA));
                funcionarioLido.setAutorizacao(resultSet.getInt(COLUMN_AUTORIZACAO));
                funcionarioLido.setId(resultSet.getInt(COLUMN_IDACESSO));
                funcionarioLido.setNome(resultSet.getString(COLUMN_NOME));
            }else{
                close();
                throw new RegraDaoException("Usuário não encontrado");
            }
        } catch (SQLException ex) {
            throw new RegraDaoException("Erro ao recuperar objeto: "
                    + ex.getMessage());
        } finally {
            close();
            return funcionarioLido;
        }
    }
    
    
     private void close() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("Erro ao fechar");
        }

    }
}
